//
//  WBStatusPictureView.swift
//  传智微博
//
//  Created by zhoujiming on 2017/2/17.
//  Copyright © 2017年 gmcc. All rights reserved.
//

import UIKit

class WBStatusPictureView: UIView {
    
    var viewModel: WBStatusViewModel?{
        didSet{
            calcViewSize()
            
            urls = viewModel?.picURLs
        }
    }
    
    /// 根据视图模型的配图大小，调整显示内容
    private func calcViewSize(){
        // 处理宽度
        // 1> 单图，根据配图视图的大小，改变subview【0】的高度
        if viewModel?.picURLs?.count == 1{
            
            let viewSize = viewModel?.pictureViewSize ?? CGSize()
            // 获取第0个图像视图
            let v = subviews[0]
            v.frame = CGRect(x: 0,
                             y: WBStatusPictureViewOutterMargin,
                             width: viewSize.width,
                             height: viewSize.height - WBStatusPictureViewOutterMargin)
        }
        else{
            // 2> 多图，恢复subview【0】的高度
            
            // 获取第0个图像视图
            let v = subviews[0]
            
            v.frame = CGRect(x: 0, y: WBStatusPictureViewOutterMargin, width: WBStatusPictureItemWidth, height: WBStatusPictureItemWidth)
        }
        
        heightCons.constant = viewModel?.pictureViewSize.height ?? 0
    }
    // 配图视图数组
    private var urls: [WBStatusPicture]?{
        didSet{
            // 隐藏所有的imageView
            for v in subviews {
                v.isHidden = true
            }
            
            // 设置图像
            var index = 0
            for url in urls ?? [] {
                // 获得索引的imageview
                let iv = subviews[index] as! UIImageView
                
                if index == 1 && urls?.count == 4{
                    index += 1
                }
                // 设置图像
                iv.cz_setImage(urlString: url.thumbnail_pic, placeholderImage: nil)
                
                iv.isHidden = false
                
                index += 1
            }
        }
    }

    @IBOutlet weak var heightCons: NSLayoutConstraint!
    
    override func awakeFromNib() {
        setupUi()
    }
}

extension WBStatusPictureView {
    // cell中所有的控件提前生成好
    // 设置的时候决定是否显示
    // 不要动态创建
    fileprivate func setupUi(){
        backgroundColor = superview?.backgroundColor
        
        // 超出边界的内容不显示
        clipsToBounds = true
        
        let rect = CGRect(x: 0, y: WBStatusPictureViewOutterMargin, width: WBStatusPictureItemWidth, height: WBStatusPictureItemWidth)
        
        // 循环创建9个imageView
        for i in 0..<9 {
            let iv = UIImageView()
            
            iv.backgroundColor = superview?.backgroundColor
            
            
            iv.contentMode = .scaleAspectFill
            iv.clipsToBounds = true
            
            let row = CGFloat(i / 3)
            let col = CGFloat(i % 3)
            
            let xOffset = col * (WBStatusPictureItemWidth + WBStatusPictureViewInnerMargin)
            let yOffset = row * (WBStatusPictureItemWidth + WBStatusPictureViewInnerMargin)
            
            iv.frame = rect.offsetBy(dx: xOffset, dy: yOffset)
            
            addSubview(iv)
        }
    }
}
